package gui;

import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public abstract class Tool {

	public abstract void select(MainFrame f);

	public abstract void keyType(MainFrame f, UI_SFG graph, KeyEvent e, boolean done);

	public abstract void mouseClick(MainFrame f, UI_SFG graph, MouseEvent e, MyView v);

	public abstract void mouseMove(MainFrame f, UI_SFG graph, MouseEvent e, int dX, int dY, MyView v);

	public abstract void mouseDrag(MainFrame f, UI_SFG graph, MouseEvent e, int dX, int dY, MyView v);

	public abstract Cursor cursor();

	public abstract void paint(MainFrame f, UI_SFG graph, Graphics2D g, MyView v);

	public abstract void clear(MainFrame f, UI_SFG graph);

	public abstract boolean isBusy();

	public UI_Node snap(MainFrame f, UI_SFG graph, Point p, UI_Node except) {
		// NODES
		for (UI_Node n : graph.nodes())
			if (n != except && p.distanceTo(n.pos()) <= f.pnlCanvas.SNAP_RADIUS) {
				p.x = n.pos().x;
				p.y = n.pos().y;
				return n;
			}
		// GRID
		if (f.pnlCanvas.showGrid) {
			float gridX = Math.round(p.x / f.pnlCanvas.GRID) * f.pnlCanvas.GRID;
			float gridY = Math.round(p.y / f.pnlCanvas.GRID) * f.pnlCanvas.GRID;
			if (p.distanceTo(gridX, gridY) <= f.pnlCanvas.SNAP_RADIUS) {
				p.x = gridX;
				p.y = gridY;
				return null;
			}
		}
		return null;
	}

	public UI_Branch snapBranch(MainFrame f, UI_SFG graph, Point p) {
		for (UI_Node ny : graph.nodes())
			for (UI_Branch b : graph.getConnections(ny))
				if (b != null && p.distanceTo(b.pos()) <= f.pnlCanvas.SNAP_RADIUS)
					return b;
		return null;
	}
}
